package features.advance.jdk.io.blocking;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Set;

/**
 * @author LIN
 * @date 2021-05-20
 */
public class BlockIO {

    public static void main(String[] args) {
        Thread client = new Thread(() -> {
            Socket socket = new Socket();
            SocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8082);
            try {
                System.out.println("开始尝试连接...");
                socket.connect(socketAddress);
                System.out.println("连接成功..."+Thread.currentThread());
                boolean connected = socket.isConnected();
                if(connected){
                    OutputStream outputStream = socket.getOutputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                    while (true) {
                        String s = bufferedReader.readLine();
                        if("exit".equals(s)){
                            break;
                        }
                        outputStream.write(s.getBytes());

                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(socket.isConnected()){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        client.start();

        Thread server = new Thread(() -> {
            ServerSocket serverSocket = null;
            try {
                serverSocket= new ServerSocket(8082);
                System.out.println("启动服务器...");
                Socket accept = serverSocket.accept();
                System.out.println("启动成功...");

                InputStream inputStream = accept.getInputStream();
                int a = -1;
                while ((a = inputStream.read()) != -1){
                    System.out.print((char)a);
                }
                System.out.println();

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (serverSocket != null){
                    if (!serverSocket.isClosed()) {
                        try {
                            serverSocket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

//        server.start();
        Thread serverChannel = new Thread(() -> {
            try {
                ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
                serverSocketChannel.configureBlocking(false);
                SocketAddress socketAddress = new InetSocketAddress(8080);
                serverSocketChannel.bind(socketAddress);

                Selector selector = Selector.open();

                serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

                while(selector.select() > 0){
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();

                    for (SelectionKey selectionKey : selectionKeys) {

                    }
                }


            } catch (IOException e) {
                e.printStackTrace();
            }

        });
        serverChannel.start();


    }
}
